package cn.wp.common.core.id;

import cn.wp.common.core.util.ApplicationContextUtil;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * ID 获取
 * Created by chenyb on 2019/09/24
 */
public class Id {

    /**
     * 生成ID
     *
     * @return
     */
    public static String gen() {
        long nextId = ApplicationContextUtil.getBean(SnowflakeIdWorker.class).nextId();
        return nextId + "";
    }

    /**
     * 生成UUID
     *
     * @return
     */
    public static String genUUID() {
        String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
        return uuid;
    }


    private static Integer number = 1;
    private static int maxNum = 200000;
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");//年月日格式

    /**
     * uuid生成没有 — 的编号
     *
     * @return
     */
    public static String createUUID() {
        String uuid = UUID.randomUUID().toString();
        return uuid.replaceAll("\\-", "");
    }

    /**
     * 生成订单编号 17+ 位数
     *
     * @return
     */
    public static String createOrderId() {
        number++;//唯一数字自增
        if (number >= maxNum) { // 值的上限，超过就归零
            number = maxNum - 200000;
        }
        return sdf.format(new Date()) + number;//返回时间+一毫秒内唯一数字的编号，区分机器可以加字母ABC...
    }

    public static void main(String[] args) {
        System.out.println(Id.createOrderId());
    }
}
